<!DOCTYPE html>
<html>

    {include file='public/header'}

    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="javascript:void(0)">首页</a>
                <a href="javascript:void(0)">充值面板管理</a>
                <a>
                    <cite>充值金额列表</cite>
                </a>
            </span>
            <a class="layui-btn layui-btn-small" id="reload" title="刷新">
                <i class="layui-icon" style="line-height:30px">
                    <i class="iconfont">&#xe6aa;</i>
                </i>
            </a>
        </div>
        <div class="x-body">
            <table id="money" lay-filter="money"></table>
        </div>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="add">新增充值项</button>
            </div>
        </script>
        <script
            type="text/html" id="switchTpl">
            <!-- 这里的 checked 的状态只是演示 -->
            <input type="checkbox" name="sex" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="sexDemo" {{ d.is_display == 1 ? 'checked' : '' }}>
        </script>
        <script type="text/html" id="tbBar">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <!-- <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a> -->
        </script>
        <script>
            let PlatformType = new Promise((resolve) => {
                Ajax('/admin/money/getPlatformType', {}).then(result => {
                    resolve(result.data)
                });
            })
            let getRechargeType = new Promise((resolve) => {
                Ajax('/admin/money/getRechargeType', {type: 1}).then(result => {
                    resolve(result.data)
                });
            })

            Promise.all([PlatformType, getRechargeType]).then((dataArray) => {
                let params = {
                    elem: '#money',
                    toolbar: '#toolbarDemo',
                    url: '/admin/money/list',

                    // 数据接口
                    // 数据接口
                    id: 'money',
                    limit: 15,
                    page: true,
                    cols: [
                        [
                            {
                                // 表头
                                field: 'id',
                                title: 'ID',
                                width: 80,
                                sort: true,
                                align: 'center'
                            },
                            {
                                title: '充值类型',
                                templet: function (d) {
                                    for (let index = 0; index < dataArray[1].length; index++) {
                                        const element = dataArray[1][index];
                                        if (element.id == d.type) {
                                            return element.name
                                        }
                                    }
                                },
                                align: 'center'
                            },
                            {
                                field: 'name',
                                title: '充值金额名',
                                align: 'center'
                            },
                            {
                                field: 'recharge_price',
                                title: '充值金额',
                                align: 'center'
                            }, {
                                field: 'send_currency',
                                title: '赠送K币数',
                                align: 'center'
                            }, {
                                field: 'sum_currency',
                                title: '总共K币数',
                                align: 'center'
                            }, {
                                field: 'send_title',
                                title: '赠送标语',
                                align: 'center'
                            }, {
                                field: 'label',
                                title: '标签',
                                align: 'center'
                            }, {
                                title: '展示对象',
                                templet: function (d) {
                                    return d.display_object == 1 ? '全部用户' : d.display_object == 2 ? '已充值用户' : '未充值用户'
                                },
                                align: 'center'
                            }, {
                                title: '平台类型',
                                templet: function (d) {
                                    for (let index = 0; index < dataArray[0].length; index++) {
                                        const element = dataArray[0][index];
                                        if (element.type == d.platform_type) {
                                            return element.name
                                        }
                                    }
                                },
                                align: 'center'
                            }, {
                                title: '是否用于公共配置',
                                templet: function (d) {
                                    return d.is_common == 1 ? '是' : '否'
                                },
                                align: 'center'
                            }, {
                                field: 'is_display',
                                title: '是否启用',
                                width: 100,
                                templet: '#switchTpl',
                                unresize: true
                            }, {
                                toolbar: '#tbBar',
                                title: '操作',
                                width: 100,
                                align: 'center'
                            }
                        ]
                    ]
                }
                tableList(params)
            })


            layui.use('table', function () {
                var table = layui.table,
                    form = layui.form;
                table.on('toolbar(money)', function (obj) {
                    var event = obj.event;
                    if (event === 'add') {
                        x_admin_show('添加充值项', './info.html', 650, 550);
                    }
                });
                // form.on('switch(sexDemo)', function (obj) {
                //     // console.log(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
                //     // layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
                //     // console.log(this.value + ' ' +  '：' + );
                //     // ChangeAjax('/admin/money/rechargeMoneySwitch',{id:this.value,display:obj.elem.checked?1:0})
                //     layer.confirm('确定修改吗！', {
                //         btn: ['确定', '取消'] // 按钮
                //     }, function () {

                //     });
                //     // let getRechargeType = new Promise((resolve) => {
                //     //     Ajax('/admin/money/getRechargeType', {type: 1}).then(result => {
                //     //         resolve(result.data)
                //     //     });
                //     // })
                // });
                form.on('switch(sexDemo)', function (obj) { // 根据业务判断是开启还是关闭
                       let getRechargeType = new Promise((resolve) => {
                        Ajax('/admin/money/rechargeMoneySwitch', {id:this.value,is_display:obj.elem.checked?1:0}).then(result => {
                            resolve(result.data)
                        });
                    })

                   
                });


                table.on('tool(money)', function (obj) {
                    var event = obj.event;
                    if (event === 'del') {
                        DeleteAjax('/admin/money/delete', {
                            id: obj.data.id
                        }, 'money')
                    } else if (event === 'edit') {
                        x_admin_show('编辑充值项', `./info.html?id=${
                            obj.data.id
                        }`, 650, 550);
                    }
                });
            });
        </script>
    </body>

</html>
